package com.carbeauty.entity;

import com.carbeauty.common.entity.BaseEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import java.math.BigDecimal;

/**
 * 美容项目实体类
 * 
 * 美容项目管理功能:
 * - 项目基本信息
 * - 价格管理
 * - 分类管理
 * - 时长管理
 * - 材料消耗管理
 * 
 * @author CarBeauty Team
 */
@Entity
@Table(name = "cb_beauty_service")
public class BeautyService extends BaseEntity {
    
    /**
     * 项目名称
     */
    @NotBlank(message = "项目名称不能为空")
    @Size(max = 100, message = "项目名称长度不能超过100个字符")
    @Column(name = "name", nullable = false, length = 100)
    private String name;
    
    /**
     * 项目分类
     */
    @NotBlank(message = "项目分类不能为空")
    @Size(max = 50, message = "项目分类长度不能超过50个字符")
    @Column(name = "category", nullable = false, length = 50)
    private String category;
    
    /**
     * 项目描述
     */
    @Size(max = 500, message = "项目描述长度不能超过500个字符")
    @Column(name = "description", length = 500)
    private String description;
    
    /**
     * 项目价格
     */
    @NotNull(message = "项目价格不能为空")
    @DecimalMin(value = "0.01", message = "项目价格必须大于0")
    @Column(name = "price", nullable = false, precision = 10, scale = 2)
    private BigDecimal price;
    
    /**
     * 会员价格
     */
    @DecimalMin(value = "0.01", message = "会员价格必须大于0")
    @Column(name = "member_price", precision = 10, scale = 2)
    private BigDecimal memberPrice;
    
    /**
     * 服务时长(分钟)
     */
    @Column(name = "duration")
    private Integer duration;
    
    /**
     * 项目图片URL
     */
    @Size(max = 200, message = "图片URL长度不能超过200个字符")
    @Column(name = "image_url", length = 200)
    private String imageUrl;
    
    /**
     * 是否热门项目
     */
    @Column(name = "is_popular")
    private Boolean isPopular = false;
    
    /**
     * 是否推荐项目
     */
    @Column(name = "is_recommended")
    private Boolean isRecommended = false;
    
    /**
     * 排序权重
     */
    @Column(name = "sort_order")
    private Integer sortOrder = 0;
    
    /**
     * 项目标签(多个标签用逗号分隔)
     */
    @Size(max = 200, message = "项目标签长度不能超过200个字符")
    @Column(name = "tags", length = 200)
    private String tags;
    
    /**
     * 适用车型
     */
    @Size(max = 100, message = "适用车型长度不能超过100个字符")
    @Column(name = "applicable_models", length = 100)
    private String applicableModels;
    
    /**
     * 注意事项
     */
    @Size(max = 500, message = "注意事项长度不能超过500个字符")
    @Column(name = "precautions", length = 500)
    private String precautions;
    
    /**
     * 积分奖励
     */
    @Column(name = "points_reward")
    private Integer pointsReward = 0;
    
    // ==================== 构造方法 ====================
    
    public BeautyService() {
        super();
    }
    
    public BeautyService(String name, String category, BigDecimal price) {
        super();
        this.name = name;
        this.category = category;
        this.price = price;
        this.memberPrice = price.multiply(new BigDecimal("0.95")); // 默认会员价为95折
    }
    
    // ==================== Getter/Setter ====================
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getCategory() {
        return category;
    }
    
    public void setCategory(String category) {
        this.category = category;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public BigDecimal getPrice() {
        return price;
    }
    
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    
    public BigDecimal getMemberPrice() {
        return memberPrice;
    }
    
    public void setMemberPrice(BigDecimal memberPrice) {
        this.memberPrice = memberPrice;
    }
    
    public Integer getDuration() {
        return duration;
    }
    
    public void setDuration(Integer duration) {
        this.duration = duration;
    }
    
    public String getImageUrl() {
        return imageUrl;
    }
    
    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
    
    public Boolean getIsPopular() {
        return isPopular;
    }
    
    public void setIsPopular(Boolean isPopular) {
        this.isPopular = isPopular;
    }
    
    public Boolean getIsRecommended() {
        return isRecommended;
    }
    
    public void setIsRecommended(Boolean isRecommended) {
        this.isRecommended = isRecommended;
    }
    
    public Integer getSortOrder() {
        return sortOrder;
    }
    
    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }
    
    public String getTags() {
        return tags;
    }
    
    public void setTags(String tags) {
        this.tags = tags;
    }
    
    public String getApplicableModels() {
        return applicableModels;
    }
    
    public void setApplicableModels(String applicableModels) {
        this.applicableModels = applicableModels;
    }
    
    public String getPrecautions() {
        return precautions;
    }
    
    public void setPrecautions(String precautions) {
        this.precautions = precautions;
    }
    
    public Integer getPointsReward() {
        return pointsReward;
    }
    
    public void setPointsReward(Integer pointsReward) {
        this.pointsReward = pointsReward;
    }
    
    // ==================== 业务方法 ====================
    
    /**
     * 获取实际价格(根据是否会员)
     */
    public BigDecimal getActualPrice(boolean isMember) {
        if (isMember && memberPrice != null) {
            return memberPrice;
        }
        return price;
    }
    
    /**
     * 计算折扣率
     */
    public BigDecimal getDiscountRate() {
        if (memberPrice != null && price.compareTo(BigDecimal.ZERO) > 0) {
            return memberPrice.divide(price, 2, BigDecimal.ROUND_HALF_UP);
        }
        return BigDecimal.ONE;
    }
    
    /**
     * 设置为热门项目
     */
    public void setPopular() {
        this.isPopular = true;
    }
    
    /**
     * 取消热门项目
     */
    public void unsetPopular() {
        this.isPopular = false;
    }
    
    /**
     * 设置为推荐项目
     */
    public void setRecommended() {
        this.isRecommended = true;
    }
    
    /**
     * 取消推荐项目
     */
    public void unsetRecommended() {
        this.isRecommended = false;
    }
    
    /**
     * 获取标签列表
     */
    public String[] getTagList() {
        if (tags != null && !tags.trim().isEmpty()) {
            return tags.split(",");
        }
        return new String[0];
    }
    
    /**
     * 设置标签列表
     */
    public void setTagList(String[] tagList) {
        if (tagList != null && tagList.length > 0) {
            this.tags = String.join(",", tagList);
        } else {
            this.tags = null;
        }
    }
}
